package com.sodaeffect.mameradost;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private static Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mContext = getApplicationContext();

		DisplaySettings();

		Switch sw = (Switch) findViewById(R.id.switch_isenabled);
		// Zmena stavu povolenia kontroly SMS
		sw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0,
					boolean buttonState) {
				// Zapisem ju do spolocnych settingov
				SharedPreferences settings = PreferenceManager
						.getDefaultSharedPreferences(mContext);
				Editor edt = settings.edit();
				edt.putBoolean(Common.PREFS_ISENABLED, buttonState);
				edt.commit();
			}
		});
	}

	private void DisplaySettings() {
		SharedPreferences settings = PreferenceManager
				.getDefaultSharedPreferences(mContext);

		Switch sw = (Switch) findViewById(R.id.switch_isenabled);
		TextView etHotNumber = (TextView) findViewById(R.id.editTextHotNumber);
		TextView etHotText = (TextView) findViewById(R.id.editTextHotText);

		Boolean isEnabled = settings.getBoolean(Common.PREFS_ISENABLED, false);
		String HotNumber = settings.getString(Common.PREFS_HOTNUMBER, "");
		String HotText = settings.getString(Common.PREFS_HOTTEXT, "");
		etHotNumber.setText(HotNumber);
		etHotText.setText(HotText);
		sw.setChecked(isEnabled);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.action_settings:
			Intent i = new Intent(this, SettingsActivity.class);
			startActivityForResult(i, 1);
			break;

		}

		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		DisplaySettings();
	}

}
